package com.joke.locomotive;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import com.jdbc.mysql.MysqlLocalConnect;

public class ImportLocal {
	/**
	 * 将保存到本地的备份txt文件，导入到本地数据库内。然后以后的数据先要跟本地库查询是否存在，不存在再上传
	 * 仅第一次全部导入
	 * @param txt
	 * @param map
	 */
	private static void readLocal() {
		
		String hot = "D:\\LocoDown\\fabu\\入库热门.txt";/*ImportHot.writeHotPath;*/
		String image = "D:\\LocoDown\\fabu\\入库趣图.txt";/*ImportImage.writeImagePath;*/
		String txt = "D:\\LocoDown\\fabu\\入库文本段子.txt";/*ImportTxt.writeTxTPath;*/
		Map<String, String> map = new HashMap<String, String>();
		try {
			readLocalHotAndImage(hot,map);
			readLocalHotAndImage(image,map);
			readLoalTxt(txt,map);
			System.out.println(map.size());
			MysqlLocalConnect.insert(map,"全部");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 仅第一次全部导入
	 * @param txt
	 * @param map
	 */
	private static void readLoalTxt(String txt, Map<String, String> map){
		Scanner scanner2;
		try {
			String content2 = "";
			FileInputStream fis2 = new FileInputStream(new File(txt));
			scanner2 = new Scanner(fis2);
			//从都一行开始读。第一行是内容
			while (scanner2.hasNextLine()) {
				content2 = scanner2.nextLine();
				if(content2!=null&&!"".equals(content2.trim())) {
					map.put(content2.trim(),"");
				}
			}
			System.out.println(map.size());
		} catch (FileNotFoundException  e) {
			e.printStackTrace();
		}
	}
	/**
	 * 仅第一次全部导入
	 * @param txt
	 * @param map
	 */
	private static void readLocalHotAndImage(String hot, Map<String, String> map) {
		Scanner scanner;
		try {
			String line = "";
			String content = "";
			String path = "";
			int l = 0;
			FileInputStream fis = new FileInputStream(new File(hot));
			scanner = new Scanner(fis);
			//从都一行开始读。第一行是内容，第二行是图片（如果没有则为空）
			while (scanner.hasNextLine()) {
				
				line = scanner.nextLine();
				if(l==0) {
					content = line.trim();
					l++;
				}else if(l==1) {
					path = line.trim();
					if(!"".equals(content.trim())) {
						map.put(content.trim(), path.trim());
					}
					content = "";
					path = "";
					l = 0;
				}
			}
			System.out.println(map.size());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		readLocal();
	}

}
